import { ref } from 'vue'
import { defineStore } from 'pinia'
import { logined } from '@/api/index'
export const useCounterStore = defineStore(
  'counter',
  () => {
    const introduction = ref<string>('')
    interface IUser {
      id: number
      account: string
      createAt: string
      username: string
      imgUrl: string
      gender: string
      tag: string
    }
    const userData = ref<IUser>({
      id: 0,
      account: '',
      createAt: '',
      username: '',
      imgUrl: '',
      gender: '',
      tag: '',
    })

    const isLogin = ref<boolean>(false)
    //  获取用户登录状态
    const loginedStatusFn = async (token: string) => {
      if (!token) {
        isLogin.value = false
        return false
      }
      try {
        const res = await logined({ token })
        if (res.data.code == 200) {
          isLogin.value = true
          return true
        } else {
          isLogin.value = false
          localStorage.removeItem('TTOO')
          return false
        }
      } catch (err) {
        console.log(err)
        isLogin.value = false
        localStorage.removeItem('TTOO')
        return false
      }
    }

    return { introduction, userData, loginedStatusFn, isLogin }
  },
  {
    persist: true,
  },
)
